package com.wy.panda.common.sign.handler;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.wy.panda.common.sign.AbstractSignValidationHandler;
import com.wy.panda.common.sign.constant.Handler;
import com.wy.panda.common.sign.constant.SignLocationEnum;
import com.wy.panda.common.sign.constant.SubmitWayEnum;

/**
 * form表单方式提交、签名信息在parameter中
 *
 * @author WangYuan
 * @since 2020年11月5日 下午3:32:10
 */
@Handler(submitWay = SubmitWayEnum.FORM, signLocation = SignLocationEnum.PARAMETER)
public class FormAndParameterSignValidationHandler extends AbstractSignValidationHandler {

	@Override
	protected String getTimestamp(HttpServletRequest request) {
		return request.getParameter(REQUEST_TIMESTAMP_KEY);
	}

	@Override
	protected String getAppKey(HttpServletRequest request) {
		return request.getParameter(REQUEST_APP_KEY);
	}

	@Override
	protected String getSignMethod(HttpServletRequest request) {
		return request.getParameter(REQUEST_SIGN_METHOD_KEY);
	}

	@Override
	protected String getSign(HttpServletRequest request) {
		return request.getParameter(REQUEST_SIGN_KEY);
	}

	@Override
	protected Map<String, String> getSignParameter(HttpServletRequest request) {
		Enumeration<String> paramNames = request.getParameterNames();
		Map<String, String> params = new HashMap<>();
		String paramName = null;
		while(paramNames.hasMoreElements()) {
			paramName = paramNames.nextElement();
			params.put(paramName, request.getParameter(paramName));
		}
		
		params.remove(REQUEST_SIGN_KEY);
		
		return params;
	}

}
